package com.opencloud.iot.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.iot.client.model.entity.IotNStreng;
import com.opencloud.iot.client.model.entity.IotStreng;
import com.opencloud.iot.provider.mapper.IotNStrengMapper;
import com.opencloud.iot.provider.mapper.IotStrengMapper;
import com.opencloud.iot.provider.service.IotNStrengService;
import com.opencloud.iot.provider.service.IotStrengService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isNull;

/**
 * 动量服务
 * @author yeyong
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class IotNStrengServiceImpl implements IotNStrengService {
    @Autowired
    private IotNStrengMapper iotNStrengMapper;

    @Override
    public IPage<IotNStreng> findListPage(PageParams pageParams,Long startTime,Long endTime) {
        IotNStreng query = pageParams.mapToObject(IotNStreng.class);
        QueryWrapper<IotNStreng> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getWatchmac()),IotNStreng::getWatchmac,query.getWatchmac());
        //queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),IotNStreng::getTimestamp, startTime, endTime);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),IotNStreng::getTimestamp, startTime, endTime);
        queryWrapper.lambda().orderByDesc(IotNStreng::getTimestamp);
        return iotNStrengMapper.selectPage(new Page(pageParams.getPage(),pageParams.getLimit()),queryWrapper);
    }

    @Override
    public List<String> getMacList(Long startTime, Long endTime) {
        return iotNStrengMapper.selectMacList(startTime,endTime);
    }


    @Override
    public List<IotNStreng> findList(Long startTime,Long endTime,String watchmac) {
        QueryWrapper<IotNStreng> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(watchmac),IotNStreng::getWatchmac,watchmac);
        //queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),IotNStreng::getTimestamp, startTime, endTime);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),IotNStreng::getTimestamp, startTime, endTime);
        queryWrapper.lambda().orderByAsc(IotNStreng::getTimestamp);
        return iotNStrengMapper.selectList(queryWrapper);
    }

}
